﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.DisplayApp.ExternalApp
{
    public  class LaserPointApp
    {
        private static LaserPointApp s_instance;

        public static LaserPointApp Instance
        {
            get
            {
                if(s_instance == null)
                {
                    s_instance = new LaserPointApp();
                    s_instance.Start();
                }
                return s_instance;
            }
        }

        public static void Run()
        {
            if (s_instance == null)
            {
                s_instance = new LaserPointApp();
                s_instance.Start();
            }
        }

        private bool _hasError;
        private Process _process;
        NamedPipeClientStream _clientPipeStream;
        StreamWriter PipeClientStreamWriter;


        private void Start()
        {
            string programName = "CPPEI.Coolzon.Tools.LaserPoint.exe";
            string exeFile = string.Format("{0}\\tools\\laserpoint\\CPPEI.Coolzon.Tools.LaserPoint.exe", AppDomain.CurrentDomain.BaseDirectory);
            string pipeName = "LaserPoint";

            Process[] processList = Process.GetProcessesByName(programName);
            foreach (var process in processList)
            {
                Common.Logger.ErrorToTag("LaserPointApp", string.Format("关闭已存在的进程pid:{0}", process.Id));
            }
            

            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = exeFile;
            try
            {
                this._process = Process.Start(info);
                if (this._process == null)
                {
                    Common.Logger.ErrorToTag("LaserPointApp", "启动外部程序失败,program:{0}, file:{1}", programName, exeFile);
                    _hasError = true;
                    return;
                }
            }
            catch (Exception ex)
            {
                Common.Logger.ErrorToTag("LaserPointApp", "启动外部程序失败,program:{0}, file:{1}, ex:{2}", programName, exeFile, ex);

                _hasError = true;
                return;
            }
            Common.Logger.DebugToTag("LaserPointApp", "启动外部程序,program:{0}, file:{1}", programName, exeFile);

            try
            {
                this._clientPipeStream = new NamedPipeClientStream("127.0.0.1", string.Format("{0}DA", pipeName), PipeDirection.Out);

                this._clientPipeStream.Connect(1000);
                this.PipeClientStreamWriter = new StreamWriter(this._clientPipeStream);
                this.PipeClientStreamWriter.AutoFlush = true;
                Common.Logger.DebugToTag("LaserPointApp", string.Format("命名管道{0}连接成功", pipeName));
            }
            catch (Exception ex)
            {
                Common.Logger.DebugToTag("LaserPointApp", string.Format("命名管道{0}连接失败,ex:{1}", pipeName, ex));
                if (PipeClientStreamWriter != null)
                {
                    this.PipeClientStreamWriter.Close();
                    this.PipeClientStreamWriter = null;
                    this._clientPipeStream.Close();
                    this._clientPipeStream = null;
                }

                _hasError = true;
                return;
            }
            _hasError = false;
        }

        public void SendShow(double x, double y)
        {
            if (_hasError)
            {
                return;
            }
            string cmdString = string.Format("SHOW:>{0},{1}", x, y);

            Common.Logger.DebugToTag("LaserPointApp", string.Format("发送指令{0}", cmdString));
            if (PipeClientStreamWriter != null)
            {
                PipeClientStreamWriter.WriteLine(cmdString);
            }
        }

        public void SendHide()
        {
            if(_hasError)
            {
                return;
            }
            string cmdString = "HIDE:>";

            Common.Logger.DebugToTag("LaserPointApp", string.Format("发送指令{0}", cmdString));
            if (PipeClientStreamWriter != null)
            {
                PipeClientStreamWriter.WriteLine(cmdString);
            }
        }

        public void Close()
        {
            if (_clientPipeStream!=null)
            {
                _clientPipeStream.Close();
                _clientPipeStream = null;
                PipeClientStreamWriter = null;
            }
            if(this._process!=null)
            {
                this._process.Kill();
            }
        }
    }
}
